package fieldgame;

import core.GameAbs;
import core.PlayerAbs;

public class Game extends GameAbs {

	private final int startBalance = 1000;
	private final int victoryBalance = 3000;
	Field[] fields;

	public Game(int numberOfPlayers) {
		// Creates the number of players
		players = new PlayerAbs[numberOfPlayers];
		for (int i = 0; i < numberOfPlayers; i++)
			players[i] = new Player(i, getStartBalance());

		// Creates the field in for this game
		this.fields = new Field[] { new Field(250, false),
				new Field(-200, false), new Field(-100, false),
				new Field(-20, false), new Field(180, false),
				new Field(0, false), new Field(-70, false),
				new Field(-60, false), new Field(-80, true),
				new Field(-90, false), new Field(650, false) };
	}

	public void reset() {
		currentPlayerIndex = 0;
		for (int i = 0; i < players.length; i++) {
			((Player) players[i]).reset(startBalance);
		}
	}

	public int getStartBalance() {
		return startBalance;
	}

	public Field getField(int fieldIndex) {
		return fields[fieldIndex];
	}

	public void enterField(int diceSum) {
		((Player) players[currentPlayerIndex]).addBalance(diceSum - 2,
				fields[diceSum - 2].getModifier());
	}

	public int getVictoryBalance() {
		return victoryBalance;
	}

	public void nextPlayer() {
		// Checks if the player is on a field which gives him an extra turn and
		// gives him an extra turn.
		if (!fields[((Player) players[currentPlayerIndex]).getFieldIndex()]
				.getExtraTurn()) {
			this.currentPlayerIndex++;
			this.currentPlayerIndex %= this.players.length;
		}
	}

	public Field[] getFields() {
		return fields;
	}

	public int getCurrentPlayerIndex() {
		return currentPlayerIndex;
	}

}
